package com.example.medicalzb.controller;

import com.example.medicalzb.param.MaterialParam;
import com.example.medicalzb.service.MaterialService;
import com.example.medicalzb.util.Msg;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/api/materials")
@Api(tags = "必备材料控制类")
@CrossOrigin
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    @GetMapping(value = "")
    public Msg getMaterialWithPage(MaterialParam materialParam){
        return materialService.getMaterialWithPage(materialParam);
    }

    @PostMapping(value = "")
    @RolesAllowed({"1"})
    public Msg saveMaterial(@RequestBody MaterialParam materialParam){
        return materialService.saveMaterial(materialParam);
    }

    @PutMapping("/{id}")
    @RolesAllowed({"1"})
    public Msg updateMaterial(@PathVariable("id") Integer id, @RequestBody MaterialParam materialParam){
        if (materialParam.getTitle() == null || materialParam.getTitle().equals("")){
            return Msg.fail().mess("必备材料标题不能为空");
        }
        if (materialParam.getMessage() == null || materialParam.getMessage().equals("")){
            return Msg.fail().mess("必备材料内容不能为空");
        }
        return materialService.updateMaterial(id,materialParam);
    }

    @DeleteMapping("/{id}")
    @RolesAllowed({"1"})
    public Msg deleteMaterial(@PathVariable("id") Integer id){
        return materialService.deleteMaterial(id);
    }
}
